package com.example.openfeign.controller;

import com.example.openfeign.clientfeign.Param;
import com.example.openfeign.clientfeign.Response;
import com.example.openfeign.clientfeign.TestFeignClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

@RestController
public class TestController {
    @Autowired
    private TestFeignClient feignClient;

    @GetMapping("/age")
    public String age(String id) throws IOException {
        Param param = new Param();
        param.setUserId(id);
        Param.Header header = new Param.Header();
        header.setAmount("648");
        param.setHeader(header);
        Response test = feignClient.test(param);
        System.out.println(test);

        XmlMapper objectMapper = new XmlMapper();
        objectMapper.writeValue(new File("E://test"), param);
        return "success";
    }

    @PostMapping(value = "/test", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    public String test(@RequestBody String param) {
        System.out.println(param);
        return "<root><age>5566</age></root>";
    }
}


